require(['grid', 'topBar', 'layout','select2'], function () {
    var editDialog,funName,$top = $('#top'), $grid = $('#grid'),reloadGridData;
    var corpId;
    //顶部栏初始化
    function topBarInit() {
        $top.topBar({
            left: [
                {
                    text: '所属企业',
                    name: 'corpId',
                    option: {
                        url: '/sysCorpInfo/getCorpTree.htm',
                        name: 'corpName',
                        idKey: 'corpId',
                        pIdKey: 'parentCorpId'
                    },
                    width: 150,
                    type: 'text-tree'
                },
                {
                    text: '所属机构',
                    name: 'qOrganId',
                    option: {
                        url: '/baseOrgan/getOrganTree.htm',
                        name: 'organName',
                        idKey: 'organId',
                        pIdKey: 'parentOrganId'
                    },
                    width: 150,
                    type: 'text-tree'
                },
                {
                    text: '姓名/员工号',
                    name: 'qDrvName',
                    width: 80,
                    type: 'text'
                },
//                {
//                    text: '员工号',
//                    name: 'qWorkEmpeEmpno',
//                    width: 80,
//                    type: 'text'
//                },
                {
                    text: '状态',
                    name: 'qSt',
                    width: 80,
                    type: 'select',
                    dict:'EMS00000'
                },
                {
                    text: '查询',
                    type: 'btn',
                    click: function () {
                    	reloadGridData();
                    }
                }

            ],
            right: [
                {
                    text: '导出',
                    type: 'export-btn',
                    authority: '',
                    item: [
                        {
                            text: 'Excel导出',
                            click: function () {
                            	exportFile('Excel');
                            }
                        },
                        {
                            text: 'PDF导出',
                            click: function () {
                            	exportFile('Pdf');
                            }
                        }
                    ]
                },
               {
                   text: '导入',
                   type: 'btn',
                   width: '600',
                   authority: '',
                   click: function () {
                   	var data = $top.getValues();
                   	// if(data.qOrganId.length > 30){
                   		getEditDialog1().dialog('exec', 'accept',data.qOrganId);
                   	// }else{
                   	// 	$.messager.alert("提示", "请选择机构！", "warning");
                   	// }
                   }
               },
                {
                    text: '删除',
                    type: 'btn',
                    width: '698',
                    authority: '',
                    click: function () {
                    	deleteObj(true);
                    }
                },
                {
                    text: '+添加',
                    type: 'btn',
                    width: '698',
                    authority: '',
                    click: function () {
                    	getEditDialog('add','新增').dialog('exec', 'reset');
                    }
                }
            ]
        });
    }

    //grid初始化
    function gridInit() {
        $grid.grid({
            url: "/baseDrvInfo/searchDriverInfoPage.htm",
            colModel: [
                {name: "drvId", label: 'drvId', key: true, hidden: true},
                {name: "drvHeadPath", label: '驾驶员头像地址', hidden: true},
                {name: "drvName",label: '驾驶员姓名', width: 100},
                {name: "organName",label: '所属机构'},
                {name: "workEmpeEmpno",label: '员工工号', width: 160,align:'right'},
                {name: "timecardCardno",label: '考勤卡卡号', width: 100,align:'right'},
                {name: "birthDate",label: '出生日期', width: 100},
                {name: "gender",label: '性别', width: 100, dict: "SEX00000"},
                {name: "driLicType",label: '驾驶证类型', width: 100,dict: "DLT00000"},
                {name: "driLicNo",label: '驾驶证号', width: 100},
                {name: "st",label: '状态', width: 100, dict: "EMS00000"},
                {name: "operate", width: 130, label: '操作', title: false,align:'center',
                    formatter: 'operArea',
                    formatoptions: [{
                        text: '编辑',
                        css:'operate-edit',
                        click: function (data) {
                        	 getEditDialog('edit','编辑').dialog('exec', 'setVal',data.drvId);
                        }
                    }, {
                        text: '删除',
                        css:'operate-delete',
                        click: function (data) {
                        	deleteObj(false,data.drvId);
                        }
                    }]
            
                }
            ],
            multiselect: true
        });
    }

    /**
     * 获取表单对话框，如果没有就先创建
     * @param option {object} 对话框配置
     * @returns {jQuery}
     */
    function getEditDialog(flag,option) {
    	funName =flag;
        option = option || {};
        if (editDialog) {
            editDialog.dialog('open');
            if (option.title) {
                editDialog.dialog('setOption', 'title', option.title);
            }
        } else {
            editDialog = $('<div>').dialog({
                title: option.title || '驾驶员信息维护',
                url: './master/setting/sysDriverInfoSetEdit.htm',
                height: 550,
                width: 720,
                autoOpen: true,
                buttons: [
                    {
                        text: '确定',
                        css: 'mr10',
                        click: function () {
                        	var _t = this;
	                       	//funName :  add or edit
                        	$(_t).dialog('exec', funName,function(){
	                		    	reload(true);
	                		   });
	                        }
                    },
                    {
                        text: '关闭',
                        css: 'btn-cancel',
                        click: function () {
                            $(this).dialog('close');
                        }
                    }
                ]
            });
        }
        return editDialog;
    }
    
    
    getEditDialog1=function(fn){
    	if(!editDialog){
	        $editDialog = $('<div>').dialog({
	        	title: '导入驾驶员信息',
	            url: './master/setting/sysImportDriverInfoSet.htm',
	            height: 400,
	            width: 600,
	            autoOpen: true,
	            buttons: [
                {
                    text: '关闭',
                    css: 'btn-cancel',
                    click: function () {
                        $(this).dialog('close');
                    }
                }
            ]
	    });	
	}
		 $editDialog.dialog('open');
			return $editDialog;
    }
    
    //删除
    function deleteObj(isBatch,objId){
    	var objIds = objId;
    	if(isBatch){
    		objIds =  $grid.grid('getGridParam', 'selarrrow').toString();
	    	if(objIds == ""){
				$.messager.alert("提示", "请至少选择一条数据!");
				return false;
			}
    	}
    	$.messager.confirm('删除', '确定删除数据吗？', 'delete', function (r) {
	        if (r) {
				$.ajaxExt({
					async:true,
		    		url : "/baseDrvInfo/delDriverInfo.htm",
					data : {'drvIds' : objIds}
				}).done(function(){
					//$.messager.alert("提示","删除成功。","success");
					//reloadGridData();
					reload();
				});
	        }
    	});
	}
    
    //查询grid
	reloadGridData = function(){
		 $grid.grid('setGridParam', {
             datatype: "JSON",
             page: 1,
             postData: $top.getValues()
         }).trigger("reloadGrid");
	};
	
	//新增修改后执行
	function reload(){
		reloadGridData();
    	close && editDialog.dialog('close');
        commonFun.reloadLeftTree();
	};
	
	//导出
	function exportFile(suffix){
		if($grid.getGridParam("reccount")>0){
			var postData=$top.parent().getValues();
    		$.exportFile("/baseDrvInfo/exportDriverInfo"+suffix+".htm",postData);
		}
		else{
			$.messager.alert("警告","请先查询数据！","info");
		}
    }
	
    //layout 布局
    $('body').layout();

    topBarInit();
    gridInit();
    
    //进页面后查询
	reloadGridData();
});